package org.groupg.project.configmanager.service;


import org.groupg.project.configmanager.model.ApiConfig;
import org.groupg.project.configmanager.model.DatabaseConfig;
import org.groupg.project.configmanager.model.SshConfig;
import org.groupg.project.configmanager.repository.ApiConfigRepository;
import org.groupg.project.configmanager.repository.DatabaseConfigRepository;
import org.groupg.project.configmanager.repository.SshConfigRepository;

import java.util.List;

public class ConfigService {
    private final DatabaseConfigRepository databaseConfigRepository;
    private final ApiConfigRepository apiConfigRepository;
    private final SshConfigRepository sshConfigRepository;

    public ConfigService() {
        this.databaseConfigRepository = new DatabaseConfigRepository();
        this.apiConfigRepository = new ApiConfigRepository();
        this.sshConfigRepository = new SshConfigRepository();
    }

    // 数据库配置相关方法
    public List<DatabaseConfig> getAllDatabaseConfigs() {
        return databaseConfigRepository.findAll();
    }

    public void saveDatabaseConfig(DatabaseConfig config) {
        // 检查名称重复
        if (config.getId() == null) {
            if (databaseConfigRepository.existsByName(config.getName())) {
                throw new IllegalArgumentException("配置名称已存在: " + config.getName());
            }
        } else {
            if (databaseConfigRepository.existsByNameExcludingId(config.getName(), config.getId())) {
                throw new IllegalArgumentException("配置名称已存在: " + config.getName());
            }
        }

        databaseConfigRepository.save(config);
    }

    public void deleteDatabaseConfig(Integer id) {
        databaseConfigRepository.delete(id);
    }

    public DatabaseConfig getDatabaseConfigById(Integer id) {
        return databaseConfigRepository.findById(id).orElse(null);
    }

    // API配置相关方法
    public List<ApiConfig> getAllApiConfigs() {
        return apiConfigRepository.findAll();
    }

    public void saveApiConfig(ApiConfig config) {
        if (config.getId() == null) {
            if (apiConfigRepository.existsByName(config.getName())) {
                throw new IllegalArgumentException("API配置名称已存在: " + config.getName());
            }
        } else {
            if (apiConfigRepository.existsByNameExcludingId(config.getName(), config.getId())) {
                throw new IllegalArgumentException("API配置名称已存在: " + config.getName());
            }
        }

        apiConfigRepository.save(config);
    }

    public void deleteApiConfig(Integer id) {
        apiConfigRepository.delete(id);
    }

    // SSH配置相关方法
    public List<SshConfig> getAllSshConfigs() {
        return sshConfigRepository.findAll();
    }

    public void saveSshConfig(SshConfig config) {
        if (config.getId() == null) {
            if (sshConfigRepository.existsByName(config.getName())) {
                throw new IllegalArgumentException("SSH配置名称已存在: " + config.getName());
            }
        } else {
            if (sshConfigRepository.existsByNameExcludingId(config.getName(), config.getId())) {
                throw new IllegalArgumentException("SSH配置名称已存在: " + config.getName());
            }
        }

        sshConfigRepository.save(config);
    }

    public void deleteSshConfig(Integer id) {
        sshConfigRepository.delete(id);
    }
}